package com.watson.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;

import com.watson.BaseFrag;
import com.watson.LoginActivity;
import com.watson.MainActivity;
import com.watson.MyApp;
import com.watson.R;
import com.watson.activity.ArgumentActivity;
import com.watson.activity.KuwActivity;
import com.watson.activity.PasswordActivity;
import com.watson.utils.LogUtils;
import com.watson.utils.SPUtils;
import com.watson.utils.StringUtils;
import com.watson.utils.ToastUtil;

public class UserFragment extends BaseFrag implements View.OnClickListener {
    private Context mContext;
    private TextView tvVersions, tvName, tvName2, lyArgument, lySn, lyPassword;
    private LinearLayout lyVersions;
    private String verName;
    private Button onBtnExit;

    @Override
    public void onAttach(@NonNull Activity activity) {
        super.onAttach(activity);
        mContext = getActivity();
    }

    @Override
    protected View initContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user, container, false);
        initData();
        return view;
    }

    private void initData() {
    }

    public void initView(View view) {
        tvVersions = view.findViewById(R.id.tvVersions);
        tvName = view.findViewById(R.id.tvName);
        tvName2 = view.findViewById(R.id.tvName2);
        lyArgument = view.findViewById(R.id.lyArgument);
        lyVersions = view.findViewById(R.id.lyVersions);
        onBtnExit = view.findViewById(R.id.onBtnExit);
        lySn = view.findViewById(R.id.lySn);
        lyPassword = view.findViewById(R.id.lyPassword);

        verName = StringUtils.getVerName(mContext);
        tvVersions.setText("版本V" + verName);
        tvName.setText(SPUtils.get(mContext, "U_Name", "") + "");
        tvName2.setText(SPUtils.get(mContext, "U_DEPARTMENT", "") + "");

        lyArgument.setOnClickListener(this);
        lyVersions.setOnClickListener(this);
        onBtnExit.setOnClickListener(this);
        tvName.setOnClickListener(v -> {
            if (StringUtils.isBaseClick()) {
                return;
            }
//            AddDialog(2);
//            Intent intent = new Intent(mContext, KuwActivity.class);
//            intent.putExtra("no", "69-A-H3");
//            intent.putExtra("type", 1);
//            startActivityForResult(intent, 1001);
        });
        lySn.setOnClickListener(v -> {
            if (StringUtils.isFastClick()) {
                return;
            }
            snDialog();
        });
        lyPassword.setOnClickListener(v -> {
            if (StringUtils.isFastClick()) {
                return;
            }
            startActivity(new Intent(mContext, PasswordActivity.class));
        });
    }

    public void AddDialog(int type) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.dialog_add, null);
        TextView tvDelete = v.findViewById(R.id.tvDelete);
        TextView tvDiaTitle = v.findViewById(R.id.tvDiaTitle);
        TextView tvSave = v.findViewById(R.id.tvSave);
        EditText etAdd = v.findViewById(R.id.etAdd);
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);//点击屏幕不消失

        if (!dialog.isShowing()) {//此时提示框未显示
            dialog.show();
        }
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

//        etAdd.setText(tvName);
        //自定义的东西
        dialog.getWindow().setContentView(v);
        tvDelete.setOnClickListener(v1 -> dialog.dismiss());
        if (type == 1) {
            tvDiaTitle.setText("修改手机号");
            etAdd.setHint("请输入手机号");
        }
        if (type == 2) {
            tvDiaTitle.setText("修改昵称");
            etAdd.setHint("请输入昵称");
        }

        tvSave.setOnClickListener(view -> {
            String add = etAdd.getText().toString().trim();
            if (type == 1) {
                if (StringUtils.isEmpty(add)) {
                    ToastUtil.show("请输入手机号");
                    return;
                }
            }
            if (type == 2) {
                if (StringUtils.isEmpty(add)) {
                    ToastUtil.show("请输入昵称");
                    return;
                }
            }

            if (type == 1) {
                SPUtils.put(mContext, "userPhone", add);
            }
            if (type == 2) {
                tvName.setText(add);
                SPUtils.put(mContext, "userName", add);
            }


            LogUtils.e("TAG", "addDisease:" + add);
            dialog.dismiss();
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.lyArgument:
                if (StringUtils.isFastClick()) {
                    return;
                }
                startActivity(new Intent(mContext, ArgumentActivity.class));
                break;
            case R.id.lyVersions:
                if (StringUtils.isFastClick()) {
                    return;
                }
                if (getActivity() != null && getActivity() instanceof MainActivity) {
                    ((MainActivity) getActivity()).get_WMS_Versions();
                }
                break;
            case R.id.onBtnExit:
                if (StringUtils.isFastClick()) {
                    return;
                }
                exitDialog();
                break;
        }
    }

    public void exitDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.dialog_exit, null);
        TextView tvDelete = v.findViewById(R.id.tvDelete);
        TextView tvSave = v.findViewById(R.id.tvSave);
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);//点击屏幕不消失

        if (!dialog.isShowing()) {//此时提示框未显示
            dialog.show();
        }
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

        //自定义的东西
        dialog.getWindow().setContentView(v);
        tvDelete.setOnClickListener(v1 -> dialog.dismiss());

        tvSave.setOnClickListener(view -> {
            SPUtils.put(mContext, "userType", false);//登录状态
            startActivity(new Intent(mContext, LoginActivity.class));
            dialog.dismiss();
            getActivity().finish();
        });
    }

    public void snDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext, R.style.dialog);
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.dialog_sn, null);
        TextView tvDelete = v.findViewById(R.id.tvDelete);
        TextView tvSave = v.findViewById(R.id.tvSave);
        TextView tvName = v.findViewById(R.id.tvName);
        AlertDialog dialog = builder.create();
        dialog.setCanceledOnTouchOutside(false);//点击屏幕不消失

        if (!dialog.isShowing()) {//此时提示框未显示
            dialog.show();
        }
        dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);

        //自定义的东西
        dialog.getWindow().setContentView(v);
        tvDelete.setOnClickListener(v1 -> dialog.dismiss());
        tvName.setText(StringUtils.getDeviceSN(mContext));

        tvSave.setOnClickListener(view -> {
            StringUtils.copyTextToClipboard(mContext, StringUtils.getDeviceSN(mContext));
            ToastUtil.show("复制成功");
            dialog.dismiss();
        });
    }
}
